package com.bolovan.infovaluta.util;

import java.io.IOException;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.bolovan.infovaluta.bean.ExchangeRateSeller;
import com.google.gson.GsonBuilder;

public class RateUtil {
	public static final Document getDocument(String urlLink) throws IOException {
		Response resp = Jsoup.connect(urlLink).userAgent(RateConstants.READER_AGENT).timeout(RateConstants.READ_TIMEOUT).execute();
		return resp.parse();
	}

	public static String toJsonString(Object o) {
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(ExchangeRateSeller.class, new ExchangeRateSellerAdapter<ExchangeRateSeller>());
		return builder.create().toJson(o);
	}
	
	public static String getNowAsLocalString(){
		DateTime dt = new DateTime(new Date());
		DateTime dtBucharest = dt.withZone(DateTimeZone.forID("Europe/Bucharest"));
		return dtBucharest.toString(DateTimeFormat.forPattern("yyyy-dd-MM HH:mm:ss"));
	}
}
